package com.hoshiicloud.mallshop.controller.goodsmanage;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.constant.DeleteStatusEnum;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.goods.entity.goods.GoodsGallery;
import com.hoshiicloud.goods.entity.sku.ExtSku;
import com.hoshiicloud.goods.entity.sku.ProductManage;
import com.hoshiicloud.goods.entity.spu.ExtSpu;
import com.hoshiicloud.goods.entity.spu.ExtSpuTags;
import com.hoshiicloud.goods.entity.spu.SpuSalesCategory;
import com.hoshiicloud.goods.param.ListSkuSpuParam;
import com.hoshiicloud.goods.param.goods.GoodsGalleryQueryParam;
import com.hoshiicloud.goods.param.sku.*;
import com.hoshiicloud.goods.param.spu.ExtSpuAddParam;
import com.hoshiicloud.goods.param.spu.ExtSpuUpdateParam;
import com.hoshiicloud.goods.param.spu.SpuSalesCategoryAddParam;
import com.hoshiicloud.goods.param.spu.SpuSalesCategoryQueryParam;
import com.hoshiicloud.goods.vo.BranchStoreGoodsVo;
import com.hoshiicloud.goods.vo.ListSpuSkuVo;
import com.hoshiicloud.mallshop.feign.ShopIdGeneratorService;
import com.hoshiicloud.mallshop.feign.ShopGoods2FeignService;
import com.hoshiicloud.mallshop.feign.ShopGoodsSpuSkuFeignService;
import com.hoshiicloud.mallshop.feign.ShopUserFeignService;
import com.hoshiicloud.mallshop.param.productmanage.PlatStoreBranchParam;
import com.hoshiicloud.mallshop.service.goodsmanage.BranchStoreProductService;
import com.hoshiicloud.user.entity.store.PlatStore;
import com.hoshiicloud.user.param.PlatStoreQueryParam;
import com.hoshiicloud.user.vo.store.PlatStoreBranchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 *  2019/8/12 15:18 qry
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods-manage/branch-store-product")
@Api(value = "商品管理-分店商品", tags = {"商品管理-分店商品"})
public class BranchStoreGoodsController {
    private final ShopUserFeignService shopUserFeignService;
    private final ShopGoodsSpuSkuFeignService shopGoodsFeignService;
    private final ShopGoods2FeignService shopGoods2FeignService;
    private final ShopIdGeneratorService idGeneratorService;
    private final RedisTemplate redisTemplate;
    private final BranchStoreProductService branchStoreProductService;

    public BranchStoreGoodsController(ShopUserFeignService shopUserFeignService, ShopGoodsSpuSkuFeignService shopGoodsFeignService, ShopGoods2FeignService shopGoods2FeignService, ShopIdGeneratorService idGeneratorService, RedisTemplate redisTemplate, BranchStoreProductService branchStoreProductService) {
        this.shopUserFeignService = shopUserFeignService;
        this.shopGoodsFeignService = shopGoodsFeignService;
        this.shopGoods2FeignService = shopGoods2FeignService;
        this.idGeneratorService = idGeneratorService;
        this.redisTemplate = redisTemplate;
        this.branchStoreProductService = branchStoreProductService;
    }

    @ApiOperation(value ="获取分店信息分页列表")
    @PostMapping("/getBranchStoresPage")
    public Response<Page<PlatStoreBranchVo>> getBranchStoresPage(@RequestBody PlatStoreBranchParam param, HttpServletRequest rq){
        Long storeId = UserInfoUtil.getUserInfo(rq).getStoreId();
        param.setParentId(storeId);
        PlatStoreQueryParam qp;
        try {
            qp = BeanUtils.covertType(param, PlatStoreQueryParam.class);
        } catch (Exception e) {
            log.error("查询平台店铺Bean转换异常", e);
            return Response.failed("查询平台店铺Bean转换异常");
        }
        return Response.data(branchStoreProductService.getBranchStoresPage(qp));
    }

    @ApiOperation(value ="查询分店商品信息")
    @PostMapping("/listBranchStoreGoods")
    public Response listBranchStoreGoods(@RequestBody BranchStoreProductPageParam param) {
        return branchStoreProductService.listBranchStoreGoods(param);
    }


    @ApiOperation(value ="分店可添加sku列表")
    @PostMapping("/getBranchStoreCanAddSkuPage")
    public Response<Page<ListSpuSkuVo>> getBranchStoreCanAddSkuPage(@RequestBody ListSkuSpuParam param, HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        param.setStoreId(user.getStoreId());
        return branchStoreProductService.getBranchStoreCanAddSkuPage(param);
    }



    @ApiOperation("总店同步商品")
    @GetMapping("/syncMainStoreProduct")
    public Response<Boolean> storeProductSync(HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        Long mainStoreId = user.getStoreId();
        if(redisTemplate.hasKey("mainStoreSyncProductToBranchStore"+mainStoreId)){
            return Response.failed("本店已有操作员在同步商品到分店，请稍后再操作");
        }
        redisTemplate.opsForValue().set("mainStoreSyncProductToBranchStore"+mainStoreId,1,1, TimeUnit.DAYS);

        String name = user.getRealName();
        LocalDateTime now = LocalDateTime.now();
        try {
            //获取所有分店数据
            PlatStoreQueryParam param = new PlatStoreQueryParam();
            param.setPageNumber("1");
            param.setPageSize("10000");
            param.setParentId(mainStoreId);
            Response<Page<PlatStore>> listPlatStore = shopUserFeignService.getPlatStorePage(param);
            //log.error("分店ID集合");
            List<Long> listBranchStoreIds = listPlatStore.getData().getRecords().stream().map(PlatStore::getId).collect(Collectors.toList());
            // log.error("分店ID集合",listBranchStoreIds.size(),"ge:",listBranchStoreIds);
            //获取分店需要添加的sku 信息
            Response<Map<String, List<String>>> storeAndBaseSkuIds = shopGoodsFeignService.queryExtskuAndBaseSkuIds(listBranchStoreIds);
            //log.error("分店BaseSkuId:{},ge:{}", storeAndBaseSkuIds.getData().size(), storeAndBaseSkuIds.getData());

            // 获取总店的商品信息
            ExtSkuQueryParam extSkuQueryParam = new ExtSkuQueryParam();
            extSkuQueryParam.setStoreId(mainStoreId);
            extSkuQueryParam.setDeleted(DeleteStatusEnum.UN_DELETED.getCode());
            Response<List<ExtSku>> listExtsku = shopGoodsFeignService.listExtSkuByParam(extSkuQueryParam);
            // log.error("总店ExtSku:{},ge{}",listExtsku.getData().size(),listExtsku.getData());

            Response<List<ExtSpu>> listExtspu = shopGoodsFeignService.listExtSpuByStoreId(mainStoreId);
            // log.error("总店ExtSpu:{}, ge{}",listExtspu.getData().size(),listExtspu.getData());

            ProductManageQueryParam productManageQueryParam = new ProductManageQueryParam();
            productManageQueryParam.setStoreId(mainStoreId);
            productManageQueryParam.setDeleted(DeleteStatusEnum.UN_DELETED.getCode());
            Response<List<ProductManage>> listProductManage = shopGoodsFeignService.listProductManageByParam(productManageQueryParam);
            //log.error("总店ProductManage:{},ge{}",listProductManage.getData().size(),listProductManage.getData());

            List<Long> listSpuIds = listExtspu.getData().stream().map(ExtSpu::getId).collect(Collectors.toList());
            Response<List<SpuSalesCategory>> listSpuSalesCategory = shopGoods2FeignService.listSpuSalesCategoryBySpuIds(listSpuIds);
            //log.error("总店SpuSalesCategory:{},ge{}",listSpuSalesCategory.getData().size(),listSpuSalesCategory.getData());


            Response<List<ExtSpuTags>> listExtSpuTags = shopGoods2FeignService.listExtSpuTagsBySpuIds(listSpuIds);
            //log.error("总店ExtSpuTags:{},ge{}", listExtSpuTags.getData().size(), listExtSpuTags.getData());

            Response<List<GoodsGallery>> listGoodsGallery = shopGoods2FeignService.listGoodsGalleryBySpuIds(listSpuIds);
            //log.error("总店GoodsGallery:{},ge{}",listGoodsGallery.getData().size(),listGoodsGallery.getData());


            List<ExtSku> allExtSku = new ArrayList<>();
            List<ExtSpu> allExtSpu = new ArrayList<>();
            List<ProductManage> allProductManage = new ArrayList<>();
            List<SpuSalesCategory> allSpuSalesCategory = new ArrayList<>();
            List<ExtSpuTags> allExtSpuTags = new ArrayList<>();
            List<GoodsGallery> allGoodsGallery = new ArrayList<>();

            for (Long branchStoreId : listBranchStoreIds) {
                // 获取指定分店的商品信息（取得的是 分店的skuId以逗号分隔的字符串）
                List<String> storeBaseSkuIdS = storeAndBaseSkuIds.getData().get(branchStoreId.toString());
                // 遍历总店的 ExtSku
                for (ExtSku extSku : listExtsku.getData()) {
                    //  如果该ExtSku在该分店不存在， 那么就得添加到该分店
                    if (null == storeBaseSkuIdS || !storeBaseSkuIdS.contains((extSku.getSkuId().toString()))) {
                        // id生成Ok
                        Long newExtSpuId = this.getId();
                        Long newExtSkuId = this.getId();

                        ExtSku brachExtsku = JSONObject.parseObject(JSONObject.toJSONString(extSku), ExtSku.class);
                        brachExtsku.setId(newExtSkuId).setSpuId(newExtSpuId)
                                .setStoreId(branchStoreId).setCreatedTime(now).setCreatedBy(name).setUpdatedTime(now).setUpdatedBy(name);
                        allExtSku.add(brachExtsku);

                        // 遍历总店的 ExtSpu
                        for (ExtSpu extSpu : listExtspu.getData()) {
                            if (extSpu.getId().longValue() == extSku.getSpuId().longValue()) {
                                ExtSpu brachExtSpu = JSONObject.parseObject(JSONObject.toJSONString(extSpu), ExtSpu.class);
                                brachExtSpu.setId(newExtSpuId).setStoreId(branchStoreId)
                                        .setCreatedTime(now).setCreatedBy(name)
                                        .setUpdatedTime(now).setUpdatedBy(name);
                                allExtSpu.add(brachExtSpu);
                            }
                        }

                        for (ProductManage productManage : listProductManage.getData()) {
                            if (productManage.getSkuId().longValue() == extSku.getId().longValue()) {
                                ProductManage brachProductManage = JSONObject.parseObject(JSONObject.toJSONString(productManage), ProductManage.class);
                                brachProductManage.setSkuId(newExtSkuId).setId(this.getId())
                                        .setStoreId(branchStoreId).setCreatedTime(now).setCreatedBy(name)
                                        .setUpdatedTime(now).setUpdatedBy(name);
                                allProductManage.add(brachProductManage);
                            }
                        }

                        for (SpuSalesCategory spuSalesCategory : listSpuSalesCategory.getData()) {
                            if (spuSalesCategory.getExtSpuId().longValue() == extSku.getSpuId().longValue()) {
                                SpuSalesCategory brachSpuSalesCategory = JSONObject.parseObject(JSONObject.toJSONString(spuSalesCategory), SpuSalesCategory.class);
                                brachSpuSalesCategory.setExtSpuId(newExtSpuId)
                                        .setId(this.getId()).setCreatedBy(name).setCreatedTime(now).setUpdatedTime(now).setUpdatedBy(name);
                                allSpuSalesCategory.add(brachSpuSalesCategory);
                            }

                        }

                        for (ExtSpuTags extSpuTags : listExtSpuTags.getData()) {
                            if (extSpuTags.getSpuId().longValue() == extSku.getSpuId().longValue()) {
                                ExtSpuTags brachExtSpuTags = JSONObject.parseObject(JSONObject.toJSONString(extSpuTags), ExtSpuTags.class);
                                brachExtSpuTags.setId(this.getId()).setSpuId(newExtSpuId).setCreatedBy(name).setCreatedTime(now).setUpdatedTime(now).setUpdatedBy(name);
                                allExtSpuTags.add(brachExtSpuTags);
                            }

                        }


                        for (GoodsGallery goodsGallery : listGoodsGallery.getData()) {
                            if (goodsGallery.getExtSpuId().longValue() == extSku.getSpuId().longValue()) {
                                GoodsGallery brachGoodsGallery = JSONObject.parseObject(JSONObject.toJSONString(goodsGallery), GoodsGallery.class);
                                brachGoodsGallery.setId(this.getId()).setExtSpuId(newExtSpuId).setCreatedTime(now).setUpdatedTime(now);
                                allGoodsGallery.add(brachGoodsGallery);
                            }
                        }
                    }

                }
            }

            this.saveALLSyncData(allExtSpu, allExtSku, allProductManage, allSpuSalesCategory, allExtSpuTags, allGoodsGallery);
        }catch (Exception e){
            log.error("总店ID："+mainStoreId+"同步失败",e);
            return Response.failed("同步失败："+e.getMessage());
        }finally {
            redisTemplate.delete("mainStoreSyncProductToBranchStore"+mainStoreId);
        }
        return Response.data(true);

    }

    private void saveALLSyncData(List<ExtSpu> allExtSpu,List<ExtSku> allExtSku,
                            List<ProductManage> allProductManage, List<SpuSalesCategory> allSpuSalesCategory,
                            List<ExtSpuTags> allExtSpuTags, List<GoodsGallery> allGoodsGallery){
        shopGoodsFeignService.saveExtSpuBatch(allExtSpu);
        shopGoodsFeignService.saveExtSkuBatch(allExtSku);
        shopGoodsFeignService.saveProductManageBatch(allProductManage);
        shopGoods2FeignService.saveSpuSalesCategoryBatch(allSpuSalesCategory);
        shopGoods2FeignService.saveExtSpuTagsBatch(allExtSpuTags);
        shopGoods2FeignService.saveGoodsGalleryBatch(allGoodsGallery);
    }





    @ApiOperation(value ="添加分店商品")
    @PostMapping("/addProduct")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId", value = "skuID", dataType = "Long"),
            @ApiImplicitParam(name = "storeId", value = "总店铺Id", dataType = "Long"),
            @ApiImplicitParam(name = "branchStoreId", value = "分店店铺Id", dataType = "Long")
    })
    @Transactional(rollbackFor = Exception.class)
    public Response addProduct(@RequestBody Map<String, Object> map, HttpServletRequest request){

        TokenUsersVO user = UserInfoUtil.getUserInfo(request);

        // 如果总店的商品在分店已经存在， 那么就不允许选择添加
        Map<String,Object> map2 = new HashMap<>();
        map2.put("skuId",map.get("skuId").toString());
        map2.put("storeId",map.get("branchStoreId").toString());
        ExtSku exist = shopGoodsFeignService.getExtSkuOneByStoreIdAndSkuId(map2).getData();
        if (null != exist) {
            throw  new ServiceException("该商品在此分店已存在无需再进行分配。");
        }
        // 获取分店Id
        Long newStoreId = Long.valueOf(map.get("branchStoreId").toString());

        // 获取需要分配的ExtSku信息
        Response<ExtSku>  skuRs = shopGoodsFeignService.getExtSkuOneByStoreIdAndSkuId(map);

        // 获取需要分配的ExtSpu信息
        map.put("spuId",skuRs.getData().getSpuId());
        Response<ExtSpu>  spuRs = shopGoodsFeignService.getExtSpuOneByStoreIdAndSpuId(map);
        // 获取需要分配的 productManage 信息
        map.put("extSkuId", skuRs.getData().getId());
        Response<ProductManage> prodRs = shopGoodsFeignService.getProductManageOneByStoreIdAndSkuId(map);
        ExtSku extSku = skuRs.getData();
        ExtSpu extSpu = spuRs.getData();
        ProductManage productManage =prodRs.getData();
        extSku.setStoreId(newStoreId).setId(null).setCreatedTime(LocalDateTime.now()).setCreatedBy(user.getRealName()).setUpdatedTime(null).setUpdatedBy(null);
        extSpu.setStoreId(newStoreId).setId(null).setCreatedTime(LocalDateTime.now()).setCreatedBy(user.getRealName()).setUpdatedTime(null).setUpdatedBy(null);
        productManage.setStoreId(newStoreId).setId(null).setCreatedTime(LocalDateTime.now()).setCreatedBy(user.getRealName()).setUpdatedTime(null).setUpdatedBy(null);
        ExtSkuAddParam param1 ;
        try {
            param1 = BeanUtils.covertType(extSku, ExtSkuAddParam.class);
        }catch (Exception e){
            log.error("保存扩展sku信息Bean转换异常",e);
            return Response.failed("保存扩展sku信息Bean转换异常");
        }

        ExtSpuAddParam param2 ;
        try {
            param2 = BeanUtils.covertType(extSpu, ExtSpuAddParam.class);
        }catch (Exception e){
            log.error("保存扩展spu信息Bean转换异常",e);
            return Response.failed("保存扩展spu信息Bean转换异常");
        }

        ProductManageAddParam param3;
        try {
            param3 = BeanUtils.covertType(productManage, ProductManageAddParam.class);
        }catch (Exception e){
            log.error("保存扩展商品分配信息Bean转换异常",e);
            return Response.failed("保存扩展商品分配信息Bean转换异常");
        }

        // 保存ExtSpu
        Response<ExtSpu> rsSaveSpu = shopGoodsFeignService.saveExtSpu(param2);
        ExtSpu extspuBranch = rsSaveSpu.getData();

        // 保存spuSalesCategory
        SpuSalesCategoryQueryParam queyparam = new SpuSalesCategoryQueryParam();
        queyparam.setExtSpuId(skuRs.getData().getSpuId());
        List<SpuSalesCategory> salesCategorys = shopGoods2FeignService.getSalesCategoryByParam(queyparam).getData();

        SpuSalesCategoryAddParam spuSalesParam = new SpuSalesCategoryAddParam();
        spuSalesParam.setBaseSpuId(salesCategorys.get(0).getBaseSpuId());
        spuSalesParam.setSalesCategoryId(salesCategorys.get(0).getSalesCategoryId());
        spuSalesParam.setExtSpuId(extspuBranch.getId());
        shopGoods2FeignService.spuSalesCategoryAdd(spuSalesParam);

        // 保存ext_spu_tags标签 , 获取总店的ExtSpuId
        List<ExtSpuTags> tagsList = shopGoods2FeignService.getExtSpuTagsBySpuId(skuRs.getData().getSpuId()).getData();
        tagsList.stream().forEach(s -> {
            s.setSpuId(extspuBranch.getId());
            s.setId(this.getId()); });
        shopGoods2FeignService.saveExtSpuTagsBatch(tagsList);

        // 保存图片关联关系
        GoodsGalleryQueryParam ggqParam = new GoodsGalleryQueryParam();
        ggqParam.setExtSpuId(skuRs.getData().getSpuId());
        List<GoodsGallery> goodsGalleryList = shopGoods2FeignService.queryGoodsGallery(ggqParam).getData();
        goodsGalleryList.stream().forEach(s -> {
            s.setExtSpuId(extspuBranch.getId());
            s.setId(this.getId()); });
        shopGoods2FeignService.saveGoodsGalleryBatch(goodsGalleryList);

        // 保存 ExtSku
        param1.setSpuId(extspuBranch.getId());
        Response<ExtSku> rsSaveSku = shopGoodsFeignService.saveExtSku(param1);
        ExtSku branchExtSku = rsSaveSku.getData();

        // 保存 ProductManage
        param3.setSkuId(branchExtSku.getId());
        shopGoodsFeignService.saveProductManage(param3);
        return Response.data(true);
    }


    @ApiOperation(value ="更新分店商品信息")
    @PostMapping("/updataBranchStoreGoods")
    public Response<Boolean> updataBranchStoreGoods(@RequestBody BranchStoreGoodsVo branchGoods) {
        // 更新Ext_sku
        ExtSkuUpdateParam extSkuParam = new ExtSkuUpdateParam();
        extSkuParam.setId(branchGoods.getExtSkuId());
        extSkuParam.setSalePrice(branchGoods.getSalePrice());
        extSkuParam.setStoreEnough(branchGoods.getStoreEnough());
        shopGoodsFeignService.extSkuUpdate(extSkuParam);

        // 更新 ExtSpu
        List<OmnipotentWrapper> wrappers = new ArrayList<>();
        wrappers.add(OmnipotentWrapper.setWrapper("eq", "id", branchGoods.getExtSpuId()));
        wrappers.add(OmnipotentWrapper.setWrapper("eq","deleted", "0"));
        ExtSpu extSpu = shopGoodsFeignService.queryOmnipotentExtSpu(wrappers).getData().get(0);

        List<OmnipotentWrapper> wrappersNew = new ArrayList<>();
        wrappersNew.add(OmnipotentWrapper.setWrapper("eq","store_id", extSpu.getStoreId()));
        wrappersNew.add(OmnipotentWrapper.setWrapper("eq","weight", branchGoods.getWeight()));
        wrappersNew.add(OmnipotentWrapper.setWrapper("ne","id", branchGoods.getExtSpuId()));
        wrappersNew.add(OmnipotentWrapper.setWrapper("eq","deleted", "0"));
        List<ExtSpu> data = shopGoodsFeignService.queryOmnipotentExtSpu(wrappersNew).getData();

        if (!data.isEmpty()) {
            throw new ServiceException("该权重已存在");
        }
        ExtSpuUpdateParam extSpuUpdateParam = new ExtSpuUpdateParam();
        extSpuUpdateParam.setId(branchGoods.getExtSpuId());
        extSpuUpdateParam.setWeight(branchGoods.getWeight());
        shopGoodsFeignService.extSpuUpdate(extSpuUpdateParam);

        // 更新ProductManger
        ProductManageUpdateParam productParam = new ProductManageUpdateParam();
        productParam.setId(branchGoods.getProductMangeId());

        // 查询总店商品数量
        productParam.setQuantity(branchGoods.getQuantity());
        shopGoodsFeignService.updateProductMangerById(productParam);
        return Response.data(true);
    }

    @ApiOperation(value ="删除分店商品信息")
    @DeleteMapping("/removeStoreGoods")
    public Response<Boolean> removeStoreGoodsById(@RequestBody BranchStoreGoodsVo branchStoreGoodsVo) {
        // 删除 productManger
        shopGoodsFeignService.deleteProductManger(branchStoreGoodsVo.getProductMangeId());
        // 删除 ExtSpu
        ExtSpuUpdateParam extSpuParam = new ExtSpuUpdateParam();
        extSpuParam.setId(branchStoreGoodsVo.getExtSpuId());
        extSpuParam.setStatus(2);
        extSpuParam.setDeleted(1);
        shopGoodsFeignService.extSpuUpdate(extSpuParam);

        // 删除 ExtSku
        ExtSkuUpdateParam extSkuParam = new ExtSkuUpdateParam();
        extSkuParam.setId(branchStoreGoodsVo.getExtSkuId());
        extSkuParam.setStatus(3);
        extSkuParam.setDeleted(1);
        shopGoodsFeignService.extSkuUpdate(extSkuParam);

        // 删除营销类目
        SpuSalesCategoryQueryParam salesParam = new SpuSalesCategoryQueryParam();
        salesParam.setExtSpuId(branchStoreGoodsVo.getExtSpuId());
        salesParam.setDeleted(0);
        SpuSalesCategory spuSalesCategory = shopGoods2FeignService.getSalesCategoryByParam(salesParam).getData().get(0);
        shopGoods2FeignService.delSalesCategory(spuSalesCategory.getId());

        // 删除标签
        shopGoods2FeignService.deleteTagBatch(branchStoreGoodsVo.getExtSpuId());


        // 删除商品图片关联
        List<Long> extSpuIds = new ArrayList<>();
        extSpuIds.add(branchStoreGoodsVo.getExtSpuId());
        shopGoods2FeignService.listGoodsGalleryBySpuIds(extSpuIds).getData().stream()
                    .map(GoodsGallery::getGalleryId).collect(Collectors.toList());
        shopGoods2FeignService.deleteGalleryByExtSpuId(branchStoreGoodsVo.getExtSpuId());
        // 删除相册表
        //shopGoodsFeignService.deleteBatchs(galleryIds);
        return Response.data(true);

    }

    @ApiOperation(value ="批量删除分店商品信息")
    @DeleteMapping("/removeBatchStoreGoods")
    public Response removeBatchStoreGoods(@RequestBody List<BranchStoreGoodsVo> storeGoodsList) {
        storeGoodsList.stream().forEach(storeGoods -> {
            // 删除 productManger
            shopGoodsFeignService.deleteProductManger(storeGoods.getProductMangeId());
            // 删除 ExtSpu
            shopGoodsFeignService.deleteExtSpu(storeGoods.getExtSpuId());
            // 删除 ExtSku
            shopGoodsFeignService.deleteExtSku(storeGoods.getExtSkuId());
        });
        return Response.data(true);
    }


    /**
     * @Author qry
     * @Date 15:11 2019/8/9
     * ID 生成器
     **/
    private Long getId(){
        String id = idGeneratorService.queryGeneratorId();
        if (StrUtil.isBlank(id)) {
            throw new ServiceException(CommonError.error("主键ID生成失败"));
        }
        return Long.valueOf(id);
    }




}
